package meta

import (
	"sync"
	"time"
)

var DefaultDB = &MockDb{
	lock: sync.RWMutex{},
	M:    map[string]*MockRow{},
}

type MockDb struct {
	lock sync.RWMutex
	M    map[string]*MockRow
}

func (db *MockDb) Save(r *MockRow) {
	db.lock.Lock()
	defer db.lock.Unlock()
	if _, ok := db.M[r.Id]; !ok {
		r.CreateAt = r.UpdateAt
	}
	db.M[r.Id] = r
}

func (db *MockDb) Get(id string) *MockRow {
	db.lock.RLock()
	defer db.lock.RUnlock()
	return db.M[id]
}

func (db *MockDb) All() map[string]*MockRow {
	db.lock.RLock()
	defer db.lock.RUnlock()
	return db.M
}

type MockRow struct {
	Id             string
	Topic          string
	Key            string
	Msg            string
	Timestamp      time.Time // only set if kafka is version 0.10+, inner message timestamp
	BlockTimestamp time.Time // only set if kafka is version 0.10+, outer (compressed) block timestamp
	CreateAt       time.Time
	UpdateAt       time.Time
}
